package org.zyzboole.base.encrypt.crypto;

import cn.hutool.core.util.URLUtil;
import com.eyelake.common.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.zyzboole.base.encrypt.crypto.config.SecretConfig;

/**
 * RSA加密解密工具类
 *
 * @author zyz
 * @date 2021年1月29日
 */
@Component
@Slf4j
public class RsaKit {

    private static SecretConfig secret;

    private RsaKit() {
    }

    @Autowired
    private void setConfig(SecretConfig config) {
        secret = config;
    }

    /**
     * 公钥-加密
     *
     * @param data
     * @return String
     * @throws Exception
     * @author zyz
     * @date 2021年1月29日
     */
    public String encrypt(String data) throws Exception {
        String rsaEncrypt = SignatureKit.rsaEncrypt(data, secret.getPublicKey(), "UTF-8");
        return rsaEncrypt;
    }

    /**
     * 私钥-解密
     *
     * @return String
     * @throws Exception
     * @author zyz
     * @date 2021年1月29日
     */
    public String decrypt(String data) throws Exception {
        String rsaDecrypt = SignatureKit.rsaDecrypt(data, secret.getPrivateKey(), "UTF-8");
        return rsaDecrypt;
    }

    /**
     * 私钥-解密
     *
     * @return String
     * @throws Exception
     * @author zyz
     * @date 2021年1月29日
     */
    public static String decryptNoEx(String data) {
        String rsaDecrypt = null;
        try {
            rsaDecrypt = SignatureKit.rsaDecrypt(data, secret.getPrivateKey(), "UTF-8");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw BizException.getInstance("参数异常");
        }
        return rsaDecrypt;
    }

    public static void main(String[] args) throws Exception {
//        String publics = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7EFljqWTl7ZBjlRbtX8mQ91RCBMBwYmTASF0BgZwuD+MGAXFnSSan9bJG4D5D7fWuHFzW7BuiNWAOiNBjWjnl6qCm9ZTEDdc02Q5qkTBThgRnAo/FUBpoMiF57DfR+69zEUg6a6bVZzZaci/wK0kXegRfm2HtqMrPdb+dsGCsgwIDAQAB";
//
//        String privates = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALsQWWOpZOXtkGOVFu1fyZD3VEIEwHBiZMBIXQGBnC4P4wYBcWdJJqf1skbgPkPt9a4cXNbsG6I1YA6I0GNaOeXqoKb1lMQN1zTZDmqRMFOGBGcCj8VQGmgyIXnsN9H7r3MRSDprptVnNlpyL/ArSRd6BF+bYe2oys91v52wYKyDAgMBAAECgYAC64Npsdg4Wpf5ZfdgpzEfSS0QhsB2QKLv+XEyN9K3SVoy2szpXtPypTwuLQvrxYgjOqAcXr0jsA+aynnuQxdKP53SSIAPFFZ59FSy9MQ8e3lAN+bMaMFv1rowpWie6Dhku/XR7+Ripec6feNIfhMhwU8GO5W2qs7ucN3OSqyz6QJBAPAmdeH5Xssxd+B8Bf+6lLepJxQCgoPDAO6bit0eEw38KAGQQzYqyKsy96PKn9tEcb/eDy6qW2wyaWHv412f4UUCQQDHaPMvjHxvDUQmtBqhLVzIateKszhmR71Ohtdi3ZuvEGU/oHfoFBAqZuJm2dUaT0NFVzoAH9wtrlueIoTmph8nAkBGGVPrpFjxQv1U/57YqOPkQHHxu1HRmEhbXffI15CEUohy/GNCGnb80iHLI8hJvs7dTLptyrho+sYcGmPp3lNpAkBNIGaOyYOHYFYvshFqraTpT4m2d8MkrDKoILdF4fjJKUo/5cz60me+PrTqmu41ktquAyLH0YrTkFCXRTqqCy6ZAkB93WSBbHC24s8EX8bz2rpaBCQy8zrUmVpFk1B+xAEMVJTZfcmw1+fTD1LQFc2n8jD6G6RRuwDxv2oTca47Vt+B";
//        String s = SignatureKit.rsaEncrypt("/platform/commonFile/2022/02/10/6204a4f5a1709fcc496fc2a6.pdf", publics, Constants.UTF8);
//        String c = SignatureKit.rsaDecrypt(s, privates, Constants.UTF8);
//        System.out.println(s);
//        System.out.println(c);
        String encode = URLUtil.encode("perihimFC2Eo9z7vTAGtt/Md+oJq0l6hJ94ajMCEZoR1s6vpa5op7SW+hBDXy7FQ2uAlqhvASSoW11CLEcr7huZwYAdkRA7gE5dCFMT+i7X/KSEOkwk8/t/MQoNBKCYfQikVL0cqNp3kptpyBgA7rgb+s2scng/dukpmfVRydc0=");
        System.out.println(encode);
        System.out.println(URLUtil.decode(encode));
    }

}
